******************************************************************************************
* Do-file name:	an_reg_employ_01.do                                                      
* Task:         run regressions on apprenticeships, municipality level
* Last change:  28.07.2025                                                               
* Notes:
/*
This file contains the code used to generate the results presented in the following tables and figures:
- Figure 3: 
		-- Native apprenticeships: emp_nat_app
*/
******************************************************************************************



******************************************************************************************
*** program setup
******************************************************************************************

version 14.2
clear all
macro drop _all
set linesize 90
set more off
* set trace on
discard
set seed 123456789
*set matsize 2000



******************************************************************************************
*** load working dataset
******************************************************************************************

use "data/employ_region_s1.dta", clear



******************************************************************************************
*** set globals
******************************************************************************************

* 1) 
global group_g_emp_nat_app "All apprentice workers (natives), GR"
global weight_emp_nat_app  "emp_nat_app_90*weight_matching"


*** use different czech shocks
foreach year of numlist 1985/1990 1992/1995 {
global czech_shock_`year' "czshare_92_90_predic_13"
	}

global czech_shock_1991   "czshare_91_90_predic_13"


******************************************************************************************
*** estimate employment change
******************************************************************************************

*** create global for loop
global emp_nat_string "emp_nat_app"


*** run different samples 				
foreach var of global emp_nat_string {

foreach y of numlist 1987/1995 {

*** 1) basic model: full sample
reg g_`var'  ${czech_shock_`y'}  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}], cluster(ao_kreis_imp)
est store `var'`y'


*** check sample
dis "Outcome Variable: ${group_g_`var'}"
dis "Year = `y'"
dis "Sample restriction version: 16-65, apprentice natives (pers_gr: 101 & 102)"
count if e(sample)


********* bootstrap standard errors ***************************************************************
preserve
keep if e(sample)
drop czshare_92_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_92_`bs'.dta", nogenerate
	reg g_`var'  czshare_92_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_92_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}]
	scalar coef`bs' = _b[czshare_92_90_predic_13]
	drop y res xb czshare_92_90_predic_13 wild
}
gen bs = . 
gen coef_g_`var'_`y' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_g_`var'_`y' = coef`bs' in `bs'
}
keep bs coef_g_`var'_`y'
keep if bs != .
save "data/bootstrap\emp_app/bs_coef_g_`var'_`y'.dta", replace
sum coef_g_`var'_`y'		// std. dev. = std. error
restore

***************************************************************************************************

********* bootstrap standard errors 1991 **********************************************************
if `y' == 1991 {
preserve
keep if e(sample)
drop czshare_91_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_91_`bs'.dta", nogenerate
	reg g_`var'  czshare_91_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_91_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}]
	scalar coef`bs' = _b[czshare_91_90_predic_13]
	drop y res xb czshare_91_90_predic_13 wild
}
gen bs = . 
gen coef_g_`var'_`y' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_g_`var'_`y' = coef`bs' in `bs'
}
keep bs coef_g_`var'_`y'
keep if bs != .
save "data/bootstrap\emp_app\1991_cz_shock/bs_coef_g_`var'_`y'.dta", replace
sum coef_g_`var'_`y'		// std. dev. = std. error
restore
}
***************************************************************************************************
	}


*** create tables
#delimit ;
global estout_employ "cells(b(star fmt(%9.3f) vacant(-)) se(fmt(%9.3f) par)) 
stats(N N_clust r2_a, labels("Observations" "Clusters" "Adjusted R2") layout(@ @ @) fmt(%9.0fc %9.0fc %9.3f))
starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30)
varlabels(_cons "Constant" czshare_92_90_predic_13 "Share Czechs (92-90, pred.)") label
mlabels("Empl. 87-90" "Empl. 88-90" "Empl. 89-90" "Empl. 91-90" "Empl. 92-90" "Empl. 93-90" "Empl. 94-90" "Empl. 95-90")
prehead(@title) posthead() postfoot(@note) nonumbers collabels(none) style(tab)";
#delimit cr


** Table: Employment
estout  `var'1987  `var'1988  `var'1989  `var'1991  `var'1992  `var'1993  `var'1994  `var'1995  ///
using "tables\employ/tab_`var'.txt", $estout_employ replace ///	
title(Apprentice Employment Change 1987-1995: Base, Sample: ${group_g_`var'}) ///
note(Notes: Regional approach. * p<0.1, ** p<0.05, *** p<0.01. ///
Data Source: German Social Security Records, border districts and matched control districts, 1987-1995.)
	}


******************************************************************************************
*** end
******************************************************************************************

exit


*========================================================================================*
Comments:
- unique identifier: vsnr_ano year
